/**
 * 扩展 easyui 的 datagrid 控件
 */

(function($){

    $.extend($.fn.datagrid.defaults,{
        loadMsg:"正在加载中，请稍后......",
        emptyMsg:"没有查询到相关数据，请重新查询......",
        autoRowHeight:false,
        rownumbers:true,
        fit:true,
        onBeforeLoad:function(params){
            params.pageNum=params.page;
            params.pageSize=params.rows;
            delete params.page;
            delete params.rows;
            //$(this).datagrid("getPanel").removeClass("padding10");
        },
        onLoadError:function(){
            $.messager.alert("提示","操作异常！","error");
        }
    });

    $.extend($.fn.datagrid.defaults.view,{
        renderRow: function (target, fields, frozen, rowIndex, rowData) {
            var opts = $.data(target, "datagrid").options;
            var cc = [];
            if (frozen && opts.rownumbers) {
                var rownumber = rowIndex + 1;
                if (opts.pagination) {
                    rownumber += (opts.pageNumber - 1) * opts.pageSize;
                }
                cc.push('<td class="datagrid-td-rownumber"><div class="datagrid-cell-rownumber">' + rownumber + '</div></td>');
            }
            for (var i = 0; i < fields.length; i++) {
                var field = fields[i];
                var col = $(target).datagrid('getColumnOption', field);
                var fieldSp = field.split(".");
                //console.log(fieldSp.length);
                var dta = rowData[fieldSp[0]];
                for (var j = 1; j < fieldSp.length; j++) {
                    if(dta){
                        dta = dta[fieldSp[j]];
                    }
                }
                if (col) {
                    // get the cell style attribute
                    var styleValue = col.styler ? (col.styler(dta, rowData, rowIndex) || '') : '';
                    var style = col.hidden ? 'style="display:none;' + styleValue + '"' : (styleValue ? 'style="' + styleValue + '"' : '');

                    cc.push('<td field="' + field + '" ' + style + '>');

                    var style = 'width:' + (col.boxWidth) + 'px;';
                    style += 'text-align:' + (col.align || 'left') + ';';
                    style += opts.nowrap == false ? 'white-space:normal;' : '';

                    cc.push('<div style="' + style + '" ');
                    if (col.checkbox) {
                        cc.push('class="datagrid-cell-check ');
                    } else {
                        cc.push('class="datagrid-cell ');
                    }
                    cc.push('">');

                    if (col.checkbox) {
                        cc.push("<input type=\"checkbox\" " + (rowData.checked ? "checked=\"checked\"" : ""));
                        cc.push(" name=\"" + field + "\" value=\"" + (dta != undefined ? dta : "") + "\">");
                    } else if (col.formatter) {
                        cc.push(col.formatter(dta, rowData, rowIndex));
                    } else {
                        cc.push(dta);
                    }

                    cc.push('</div>');
                    cc.push('</td>');
                }
            }
            return cc.join('');
        }
    });

    $.extend($.fn.datagrid.methods,{
        query:function(jq,form){
            var json=$(form).serializeJSON();
            return jq.each(function(){
                $(this).datagrid("load",json);
            });
        },
        deleteChecked:function(jq,url){
            return jq.each(function(){
                var $datagrid=$(this);
                var checked=$datagrid.datagrid("getChecked");
                var length=checked.length;
                if(length==0){
                    $.messager.alert("提示","请选择一条数据！","warning");
                }else{
                    $.messager.confirm("提示","确定删除选中数据？",function(r){
                        if(r){
                            var ids=[];
                            for(var i=0;i<length;i++){
                                ids.push(checked[i].id);
                            }
                            $.post(url,{ids:ids.join(",")},function(data){
                                $.messager.show({
                                    title:"提示",
                                    msg:"操作成功！"
                                });
                                $datagrid.datagrid("load");
                            });
                        }
                    });
                }
            });
        },
        deleteSelections:function(jq,url){
            return jq.each(function(){
                var $datagrid=$(this);
                var selections=$datagrid.datagrid("getSelections");
                var length=selections.length;
                if(selections.length==0){
                    $.messager.alert("提示","请选择一条数据！","warning");
                }else{
                    $.messager.confirm("提示","确定删除选中数据？",function(r){
                        if(r){
                            var ids=[];
                            for(var i=0;i<length;i++){
                                ids.push(selections[i].id);
                            }
                            $.post(url,{ids:ids.join(",")},function(data){
                                $.messager.show({
                                    title:"提示",
                                    msg:"操作成功！"
                                });
                                $datagrid.datagrid("load");
                            });
                        }
                    });
                }
            });
        }
    });

})(jQuery);